
layui.define(['form', 'layedit', 'laydate', 'transfer'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    zmnLayUtil.layFormVerify(form);
    // 提交
    form.on('submit(zmn-service-group-form-submit)', function (obj) {
        let data = obj.field;
        let checked =  [];
        layui.$('input[type=checkbox]:checked').each(function() {
            checked.push($(this).val());
        });
        if(checked.length<1){
            layer.alert("请选择要复制的分组");
            return false;
        }
        data.groupIds=checked;
        $.ajax({
            type: "POST",
            url: "/service/group/copyGroupData.action",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (json) {
                if (json.status === 200) {
                    parent.reload(json.message);
                    cancel();
                }else {
                    parent.layer.msg(json.message,{"icon":2});
                }
            }
        });
        return false
    });
    //价格表联动
    form.on("select(selectTariffId)", function (obj) {
        layui.$("#groupDiv").html("");
        if (zmnUtil.isNull(obj) || obj.value == 0||obj.value == '') {
            return;
        }
        let tariffId = obj.value;
        let servCategId = layui.$("#servCategId").val();
        let categOneId = layui.$("#categOneId").val();
        $.ajax({
            type: "GET",
            url: "/service/group/loadGroup?tariffId="+tariffId+"&servCategId="+servCategId+"&categOneId="+categOneId,
            dataType: "json",
            success: function (res) {
                if(res.statusBool&&res.data){
                    layui.$("#groupDiv").html(loadDataToCheckboxHtml(res.data));
                }
                form.render("checkbox");
            }
        });
    });


    function loadDataToCheckboxHtml(items) {
        let initHtml = "";
        items.forEach(e=>{
            initHtml = initHtml+'<input type="checkbox" name="groupIds" value="'+e.groupId+'" title="'+e.groupAlias+'<'+e.groupId+'>'+'">';
        });
        return initHtml;
    }
    //对外暴露的接口
    exports('serv_service_group_copy', {});
});

function cancel() {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}